package org.eclipse.epsilon.emc.rdbms.view.workbench;

//import declarations
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.eclipse.epsilon.emc.rdbms.core.persistancebroker.PersistanceBroker;
import org.eclipse.epsilon.emc.rdbms.core.persistanceobject.PersistanceObject;


public class SimulatorGUI extends JFrame implements SimGUIInterface
{
	private Simulator sim;
    private JLabel ErrorMessage; 
    private JLabel EOLTitle; 
    private DefaultListModel<String> TableListModel;
    
    public static void main(String[] args) throws Exception
    {
        Simulator sim = new Simulator();        
        SimulatorGUI gui = new SimulatorGUI(sim);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(800,600);
        sim.setGUI(gui);                
        gui.setVisible(true);
    }
	
	
	
//decalre Objects
	
	
	public SimulatorGUI(Simulator simToUse) throws Exception
    {
		super("Simulator GUI");
        setLayout(new FlowLayout());
        this.sim = simToUse;	
	
        JPanel EOLEntryPanel = new JPanel();    //create the JPanel
        EOLEntryPanel.setLayout(new GridLayout(1, 1, 1, 1));    //Creates a grid layout with the specified number of rows and columns with necessary gap h and w
        final JTextArea EOLScriptArea = new JTextArea();
        final JTextArea ta            = new JTextArea(); 
        EOLEntryPanel.add(EOLScriptArea);
      
        //Now we add the information Panel
        JPanel informationPanel = new JPanel();
        informationPanel.setLayout(new GridLayout(3, 1, 2, 2));
        String[] informationOrder = {"EOLMessage","ErrorMessage"};
        
        for (int i = 0; i < informationOrder.length; i++) 
        {
            
            if(informationOrder[i].equals("ErrorMessage"))
            {
            	ErrorMessage = new JLabel();	
            	informationPanel.add(ErrorMessage); 
            }
            if(informationOrder[i].equals("ErrorMessage"))
            {
            	EOLTitle = new JLabel();	
            	EOLTitle.setText("Please enter the EOL you wish to execute below:"); 
                informationPanel.add(EOLTitle);
            }
        }
        
      //Now we add the new operational Buttons.
        JPanel operationPanel = new JPanel();
        operationPanel.setLayout(new GridLayout(2, 1, 2, 2));
        String[] operationOrder = {"Submit","Exit"};
        for (int i = 0; i < operationOrder.length; i++) {
            JButton b = new JButton(operationOrder[i]);
            b.addActionListener(new ActionListener()
            {
				public void actionPerformed(ActionEvent e)
            	{
            		if(e.getActionCommand().equals("Submit"))
             		{	
            			try 
            			{
                            ta.setText("Console Output:"+"\n");
							Object test = sim.sendCommand("epsilon",EOLScriptArea.getText());
							updateInfoPanel("Successfully ran command please review the results");
							//test.getClass().getSimpleName();
						} catch (Exception e1) 
						{
							updateInfoPanel("An Error has occured while running the specified command");
							e.getSource();
						}            				
             		}
            		else if(e.getActionCommand().equals("Exit"))
            		{
            			dispose();
            			System.exit(0);
               		}
            	}
           });
            operationPanel.add(b);
        }
        
      //Now we add the configuration Panels.
      JPanel configurationPanel = new JPanel();
      configurationPanel.setLayout(new GridLayout(1, 3, 3, 3));
      PersistanceBroker get_db_info = new PersistanceBroker();
     // ResultSet tables = get_db_info.retrieveTypes();
      
     	TableListModel = new DefaultListModel<String>();
    	TableListModel.clear();  //Clear the current TableList.
		//while(tables.next())
		  //{
		      //TableListModel.addElement(tables.getString("TABLE_NAME"));
		  //}
	   JList TableList;
      TableList = new JList(TableListModel);
      JScrollPane tablepane = new JScrollPane(TableList);
      
      configurationPanel.add(tablepane);
      //TableList.setSelectionMode(ListSelectionModel.);
      TableList.addListSelectionListener(new ListSelectionListener() 
      {
     	  boolean appended = false;
          public void valueChanged (ListSelectionEvent e) 
          {
          	{	
          	if(appended==false)
          	{
          		//EOLScriptArea.append(TableList.getSelectedValue().toString());
          	appended=true;
          	}
          	}
          }
      }); 
      
      //Now we need to add a Panel to Stream the console output to the GUI for testing.
      JPanel outputPanel = new JPanel();
      outputPanel.setLayout(new GridLayout(1, 1, 1, 1));
      JLabel header = new JLabel();
      header.setText("Testing");
      header.setSize(800, 50);
      //outputPanel.add(header); 
      
      
      ta.setRows(10);
      ta.setMinimumSize(getSize());
      OutputStream taos = new outputStream( ta, 1000 ); 
      PrintStream ps = new PrintStream( taos ); 
      System.setOut(ps); 
      System.setErr(ps); 
      
      outputPanel.add(new JScrollPane(ta),BorderLayout.SOUTH); 
      ta.setText("Console Output:"+"\n");
      ta.setEditable(false);
        //Now we need to add the various Panels to the Main content Panel and display.
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout(3, 3));
        //content.add(configurationPanel, BorderLayout.EAST);
        content.add(informationPanel, BorderLayout.NORTH);
        content.add(operationPanel, BorderLayout.EAST);
        content.add(EOLEntryPanel, BorderLayout.CENTER);
        content.add(outputPanel,BorderLayout.SOUTH);
        
        this.setContentPane(content);
        this.pack();
        this.setTitle("EOL -  WorkBench");
        this.setResizable(false);
        this.setLocationRelativeTo(null); 
    }
	
	private void updateInfoPanel(String Error)
	{
		
		ErrorMessage.setText(Error);
		notifySimHasChanged();
	}
	
	
	
	public void notifySimHasChanged() 
	{
		// TODO Auto-generated method stub
	}

}
